/**
 *
 */
package org.suren.littlebird.actions.bundle;

import java.util.List;

import org.eclipse.ui.console.MessageConsoleStream;
import org.suren.littlebird.console.BundleConsoleFactory;
import org.suren.littlebird.server.BundleServer;
import org.suren.littlebird.server.BundleServerFactory;

/**
 * @author suren
 *
 */
public class DefaultSuRenBundleController implements SuRenBundleController
{
	private BundleServer server;

	private long[] idArray;
	private String[] pathArray;

	private MessageConsoleStream msgStream = BundleConsoleFactory.getConsole().newMessageStream();

	public DefaultSuRenBundleController()
	{
		msgStream.setActivateOnWrite(false);
	}

	@Override
	public int control(int action, List<String> pathList, long... ids)
	{
		int counter = 0;

		if(ids == null || ids.length == 0)
		{
			return counter;
		}

		this.idArray = ids;
		this.server = BundleServerFactory.getInstance();
		if(server == null)
		{
			return counter;
		}

		switch(action)
		{
			case START:
				counter = start();
				break;
			case STOP:
				counter = stop();
				break;
			case INSTALL:
				if(pathList == null || pathList.size() == 0)
				{
					break;
				}

				pathArray = pathList.toArray(new String[]{});
				counter = install();
				break;
			case UNINSTALL:
				counter = uninstall();
				break;
			case UPDATE:
				counter = update();
				break;
		}

		msgStream.println("counter : " + counter);

		return counter;
	}

	/**
	 * @return
	 *
	 */
	private int update()
	{
		return server.update(idArray);
	}

	/**
	 * @return
	 *
	 */
	private int uninstall()
	{
		return server.uninstall(idArray);
	}

	/**
	 * @return
	 *
	 */
	private int install()
	{
		return server.install(pathArray);
	}

	/**
	 * @return
	 *
	 */
	private int stop()
	{
		return server.stop(idArray);
	}

	/**
	 * @return
	 *
	 */
	private int start()
	{
		return server.start(idArray);
	}
}
